package com.example.yx1.demo37;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.*;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata;

import java.util.List;

public class A37 {
    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean("config",Config.class);
        context.getDefaultListableBeanFactory().setAllowBeanDefinitionOverriding(false);
        context.registerBean(ConfigurationClassPostProcessor.class);
        context.refresh();

        for (String name : context.getBeanDefinitionNames()) {
            System.out.println(name);
        }
    }

    //导入第三方配置类
    @Configuration
    @Import(MyImportSelector.class)
    static class Config {

    }

    //负责引入用配置文件配置的外部配置类
    static class MyImportSelector implements DeferredImportSelector {

        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            List<String> names = SpringFactoriesLoader.loadFactoryNames(MyImportSelector.class, null);
            return names.toArray(new String[0]);
        }
    }

    //第三方配置类
    @Configuration
    static class AutoConfig {
        @Bean
        @ConditionalOnMissingBean
        public Bean1 bean1() {
            return new Bean1();
        }
    }

    //第三方配置类
    @Configuration
    static class AutoConfig1 {
        @Bean
        public Bean2 bean2() {
            return new Bean2();
        }
    }


    static class Bean1 {}
    static class Bean2 {}
}
